# -*- coding: utf-8 -*-
"""
# @Time : 2021/1/29 16:20 

# @Author : 一蓑烟雨任平生

# @File : 搜狗电影.py 

# @Software: PyCharm
"""

import requests
from bs4 import BeautifulSoup
import re
import time
import random

# 浏览器
agent1 = 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36'
agent2 = 'Mozilla/5.0 (iPhone; CPU iPhone OS 6_1_4 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) CriOS/27.0.1453.10 Mobile/10B350 Safari/8536.25'
agent3 = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36'
agent4 = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.一缕青丝伴忧愁 (KHTML, like Gecko) Ubuntu/一缕青丝伴忧愁.10 Chromium/27.0.1453.93 Chrome/27.0.1453.93 Safari/537.36'
agent5 = 'Mozilla/5.0 (Linux; Android 4.0.4; Galaxy Nexus Build/IMM76B) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.133 Mobile Safari/535.19'
agent6 = 'Mozilla/5.0 (Linux; Android 4.1.2; Nexus 7 Build/JZ054K) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19'
agent7 = 'Mozilla/4.0 (Windows; MSIE 6.0; Windows NT 5.2)'
agent8 = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)'
agent9 = 'Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0)'
agent10 = 'Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)'
agent11 = 'Mozilla/5.0 (compatible; WOW64; MSIE 10.0; Windows NT 6.2)'
agent12 = 'Mozilla/5.0 (Windows NT 6.3; Trident/7.0; rv 一缕青丝伴忧愁.0) like Gecko'
agent13 = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36Edge/13.10586'
list_agent = [agent1, agent2, agent3, agent4, agent5, agent6, agent7, agent8, agent9, agent10, agent11, agent12,
              agent13]

# 设置随机请求头
header = {'user-Agent': random.choice(list_agent)}

# 爬取的类别及年份
tag_moviestyle = ["喜剧", "爱情", "动作", "恐怖", "科幻", "惊悚", "犯罪", "奇幻", "战争", "悬疑", "动画", "文艺", "传记", "歌舞", "古装", "警匪", "其他"]
tag_movieyear = ["2021", "2020", "2019", "2018", "2017", "2016", "2015", "2014", "2013", "更早"]

# 类别循环
for num_moviestyle in range(0, len(tag_moviestyle)):

    # 年份循环循环
    for num_movieyear in range(0, len(tag_movieyear)):

        # 换页循环
        for page in range(1, 100):
            tag_url = "https://tv.sogou.com/list?style=" + str(tag_moviestyle[num_moviestyle]) + "&zone=&year=" + str(
                tag_movieyear[num_movieyear]) + "&starring=&fee=&order=&emcee=&req=list&query=&entity=film&page=" + str(
                page) + "&pagesize=30"

            # 请求电影列表
            rep_soall = requests.get(tag_url, headers=header)
            rep_soall = BeautifulSoup(rep_soall.text, 'html.parser')
            video_item = rep_soall.find_all("div", class_="sort_lst_tit")
            if video_item != []:
                for num_movie in range(0, len(video_item)):
                    time.sleep(3)

                    # 打印搜狗地址
                    link_video = "https://tv.sogou.com/" + video_item[num_movie].a['href']
                    print("搜狗地址： " + link_video)

                    # 打印电影名字https://www.mgtv.com/b/339347/10895269.html?cxid=94n3624ea&fromvsogou=1
                    rep_link_video = requests.get(link_video, headers=header)
                    rep_link_video = BeautifulSoup(rep_link_video.text, 'html.parser')
                    movie_name = rep_link_video.find_all("h1", class_="srch-detail-tit")[0].text
                    movie_name = re.findall(".*", movie_name)
                    movie_name = movie_name[2].replace(" ", "")
                    print("电影： " + movie_name)

                    # 打印电影基本信息
                    all_movie_message = ''
                    for movie_message in rep_link_video.find_all("dl", class_="srch-result-info")[0].stripped_strings:
                        movie_message = movie_message.replace(" ", "")
                        all_movie_message = all_movie_message + movie_message + " "
                    all_pat = re.findall(r"\D\D：", str(all_movie_message))
                    for num_pat in range(1, len(all_pat)):
                        all_movie_message = re.sub(all_pat[num_pat], "\n" + all_pat[num_pat], str(all_movie_message))
                    all_movie_message = re.sub("简介.*", "", all_movie_message).strip()
                    print(all_movie_message)

                    # 打印电影简介
                    all_movie_description = rep_link_video.head.children
                    for movie_description in all_movie_description:
                        try:
                            if movie_description['name'] == "description":
                                print("介绍： " + movie_description['content'])
                        except:
                            pass

                    # 打印电影时长
                    try:
                        movie_time = rep_link_video.find_all("div", class_="srch-detail-thumb-tit")[0].text
                        movie_time = movie_time.replace(" ", "").strip()
                        print("时长: " + movie_time)
                    except:
                        pass

                    # 打印电影评分
                    try:
                        movie_grade = rep_link_video.find_all("em", class_="stress")[0].text
                        movie_grade = movie_grade.replace(" ", "").strip()
                        print("评分: " + movie_grade)
                    except:
                        pass

                    # 打印是否付费
                    try:
                        movie_pay = rep_link_video.find_all("div", class_="sort_lst_thumb_tag_pay")[0].text
                        movie_pay = movie_pay.replace(" ", "").strip()
                        print("费用: " + movie_time)
                    except:
                        pass

                    # 打印电影海报地址
                    movie_photo = rep_link_video.find_all("div", class_="srch-detail-wrap")[0].img["src"]
                    print("图片: " + movie_photo)

                    # 打印播放平台及地址
                    for jumpplayurl in rep_link_video.find_all("div", class_="detail_section_btom")[0].div.children:
                        if str(jumpplayurl) != " ":
                            play_name = jumpplayurl.text
                            play_name = play_name.replace(" ", "").strip()
                            if re.findall("更多", play_name) != ["更多"]:
                                rep_real_link = requests.get("https://tv.sogou.com" + jumpplayurl['href'],
                                                             headers=header)
                                try:
                                    real_link = re.findall("window.open(.*?);", rep_real_link.text)[0]
                                    real_link = real_link.replace("', '_self')", "")
                                    real_link = real_link.replace("('", "")
                                    print(play_name + r"播放地址: https://jx.618g.com/?url=" + real_link)
                                except:
                                    pass
                            else:
                                for more_jumpplayurl in rep_link_video.find_all("ul",
                                                                                class_="detail_section_share_box detail_section_more_box")[0].children:
                                    more_play_name = more_jumpplayurl.text
                                    more_play_name = more_play_name.replace(" ", "").strip()
                                    rep_more_real_link = requests.get(
                                        "https://tv.sogou.com" + more_jumpplayurl.a['href'], headers=header)
                                    try:
                                        more_real_link = re.findall("window.open(.*?);", rep_more_real_link.text)[0]
                                        more_real_link = more_real_link.replace("', '_self')", "")
                                        more_real_link = more_real_link.replace("('", "")
                                        print(more_play_name + r"播放地址: https://jx.618g.com/?url=" + more_real_link)
                                    except:
                                        pass
                    print("")
            else:
                break
